﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using KINLO.Model;
using MahApps.Metro.Controls;
using KINLO.Model.InfoModel;
using KINLO.UI.Main.MES;
using KINLO.UI.Main.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using KINLO.DRV.Machine;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System.Collections.Concurrent;
using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging;

namespace KINLO.UI.Main.VM
{
    public class MesInfoVM : ViewModelBase
    {
        MesInfoView View;
        public MesInfoVM(MesInfoView view)
        {
            this.View = view;
            //班次列表
            ClassList = new ObservableCollection<ComboxItem>()
            {
                new ComboxItem(){Name="白班",Value="1"},
                new ComboxItem(){Name="夜班",Value="2"},
            };
            //班组列表
            ClassTeamList = new ObservableCollection<ComboxItem>()
            {
                new ComboxItem(){Name="A班",Value="1"},
                new ComboxItem(){Name="B班",Value="2"},
                new ComboxItem(){Name="C班",Value="3"},
            };
            Init();
        }
        public string UserName { get; set; }
        public string EquipmentCode { get; set; }
        public string process_id { get; set; }
        public string Class { get; set; }
        public string ClassTeam { get; set; }
        public string WorkOrder { get; set; }
        public string BatchNo { get; set; }
        public string EmpNo { get; set; }
        public ObservableCollection<ComboxItem> Workorders { get; set; }
        public ObservableCollection<ComboxItem> ClassList { get; set; }
        public ObservableCollection<ComboxItem> ClassTeamList { get; set; }
        public List<string> lstBill_No = new List<string>();
        public List<string> lstMaterial_Code = new List<string>();
        public List<string> lstMaterial_Spec = new List<string>();
        public List<string> lstPC_No = new List<string>();
        public List<string> lstWorkOrderStatus_Name = new List<string>();
        public List<string> lstPlanedStart = new List<string>();
        public List<string> lstPlanedEndDate = new List<string>();
        public List<string> lstProductionOrderNO = new List<string>();
        void Init()
        {
          
            //InfoDataModel.Tmp.Load();
            UserName = InfoDataModel.Tmp.UserName;
            EquipmentCode = InfoDataModel.Tmp.EquipmentCode;
            Class = InfoDataModel.Tmp.Class;
            ClassTeam = InfoDataModel.Tmp.ClassTeam;
            process_id = InfoDataModel.Tmp.process_id;
            WorkorderSelectedValue = InfoDataModel.Tmp.WorkOrder;
            BatchNo = InfoDataModel.Tmp.BatchNo;
            EmpNo = InfoDataModel.Tmp.EmpNo;
            Workorders = new ObservableCollection<ComboxItem>();
         
            DoCmdWorkOrder();
        }
        RelayCommand cmdSave;
        public RelayCommand CmdSave
        {
            get { return cmdSave ?? (cmdSave = new RelayCommand(() => DoCmdSave())); }
        }
        void DoCmdSave()
        {
            try
            {
                //InfoDataModel.Tmp.UserName = UserName;
                //InfoDataModel.Tmp.EquipmentCode = EquipmentCode;
                //InfoDataModel.Tmp.Class = Class;
                //InfoDataModel.Tmp.ClassTeam = ClassTeam;
                //InfoDataModel.Tmp.WorkOrder = WorkorderSelectedValue;
                //InfoDataModel.Tmp.BatchNo = BatchNo;
                //InfoDataModel.Tmp.EmpNo = EmpNo;

                //是否切换工单
                bool isChangeWorkOrder = InfoDataModel.Tmp.WorkOrder != WorkorderSelectedValue;
                //是否切换班组或者班次
                bool isChangeShift = (InfoDataModel.Tmp.Class != Class || InfoDataModel.Tmp.ClassTeam != ClassTeam);
                //是否切换工号
                bool isChangeEmployeeId = InfoDataModel.Tmp.UserName != UserName;


                //切换工单OR班组OR班次前 发送消息调用产量接口
                if (isChangeWorkOrder || isChangeShift || isChangeEmployeeId)
                {
                    Messenger.Default.Send("6", "UpdateTeamShiftAndWorkOrder");
                    //this.MESCountUpLoad_Hour();
                    //MyGrowl.SuccessGlobal("切换工单或班组班次成功");
                }

                InfoDataModel.Tmp.UserName = UserName;
                InfoDataModel.Tmp.EquipmentCode = EquipmentCode;
                InfoDataModel.Tmp.Class = Class; //班次
                InfoDataModel.Tmp.ClassTeam = ClassTeam; //班组
                InfoDataModel.Tmp.WorkOrder = WorkorderSelectedValue; //工单号
                InfoDataModel.Tmp.BatchNo = PC_No;  //批次号
                InfoDataModel.Tmp.process_id = process_id; //工序
                InfoDataModel.Tmp.MaterialCode = Material_Code; //料号
                InfoDataModel.Tmp.MaterialSpec = Material_Spec; //型号
                InfoDataModel.Tmp.WorkOrderStatusName = WorkOrderStatus_Name; //工单状态
                InfoDataModel.Tmp.PlanedStart = PlanedStart; //计划开始时间
                InfoDataModel.Tmp.PlanedEndDate = PlanedEndDate; //计划结束时间
                InfoDataModel.Tmp.ProductionOrderNO = ProductionOrderNO; //订单号
                InfoDataModel.Tmp.EmpNo = EmpNo;

                if (InfoDataModel.Tmp.Save())
                {
                    MessageBox.Show($"保存成功!");
                }
                else
                {
                    MessageBox.Show($"保存失败!");
                }
                //切换工单后 发送消息调用标准参数接口获取标准参数
                if (isChangeWorkOrder)
                {
                    Messenger.Default.Send(InfoDataModel.Tmp.WorkOrder, "CmdUpdateWorkOrder");
                    //this.MESCountUpLoad_Hour();
                    //MyGrowl.SuccessGlobal("切换工单或班组班次成功");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败!错误信息:{ex.Message}");
            }
        }

        //CmdCancel
        RelayCommand cmdCancel;
        public RelayCommand CmdCancel
        {
            get { return cmdCancel ?? (cmdCancel = new RelayCommand(() => DoCmdCancel())); }
        }
        void DoCmdCancel()
        {
            this.View.Close();
        }
        //CmdUserCheck
        RelayCommand cmdUserCheck;
        public RelayCommand CmdUserCheck
        {
            get { return cmdUserCheck ?? (cmdUserCheck = new RelayCommand(() => DoCmdUserCheck())); }
        }
        void DoCmdUserCheck()
        {
            if (InfoDataModel.Tmp.EnbleMesOnOff)
            {
                var result = InjectMes.MESUserNameCheck(UserName);

                if (result == 1)
                {
                    //工单校验成功给pcl写1
                    Messenger.Default.Send(1, "UpdateUserNameCheck");
                    MessageBox.Show("工号校验成功!");
                }
                else
                {
                    //工单校验失败给pcl写3
                    //Messenger.Default.Send(1, "UpdateUserNameCheck");
                    MessageBox.Show("工号校验失败！！！");
                }
            }
            else
            {
                MessageBox.Show($"MES未打开，请先打开MES开关!");
            }

        }
        //CmdWorkOrder
        RelayCommand cmdWorkOrder;
        public RelayCommand CmdWorkOrder
        {
            get { return cmdWorkOrder ?? (cmdWorkOrder = new RelayCommand(() => DoCmdWorkOrder())); }
        }
        void DoCmdWorkOrder()
        {
            try
            {
                Workorders.Clear();
                lstBill_No = new List<string>();
                lstMaterial_Code = new List<string>();
                lstMaterial_Spec = new List<string>();
                lstPC_No = new List<string>();
                lstWorkOrderStatus_Name = new List<string>();
                lstPlanedStart = new List<string>();
                lstPlanedEndDate = new List<string>();
                lstProductionOrderNO = new List<string>();
                var result = InjectMes.MESGetWorkOrder(EquipmentCode, process_id);
                if (result != null)
                {
                    for (int i = 0; i < result.Count; i++)
                    {
                        Workorders.Add(new ComboxItem { Name = result[i].Bill_No, Value = result[i].Bill_No });
                        lstBill_No.Add(result[i].Bill_No);
                        lstMaterial_Code.Add(result[i].Material_Code.ToString());
                        lstMaterial_Spec.Add(result[i].Material_Spec);
                        lstPC_No.Add(result[i].PC_No);
                        lstWorkOrderStatus_Name.Add(result[i].WorkOrderStatus_Name);
                        lstPlanedStart.Add(result[i].PlanedStart.ToString("yyyy-MM-dd HH:mm:ss"));
                        lstPlanedEndDate.Add(result[i].PlanedEndDate.ToString("yyyy-MM-dd HH:mm:ss"));
                        lstProductionOrderNO.Add(result[i].ProductionOrderNO);
                    }
                    this.View.Cboxwork.BeginInvoke(new Action(() =>
                    {
                        this.View.Cboxwork.ItemsSource = Workorders;
                    }));
                    DoCmdOrderSelectionChanged();
                    MessageBox.Show($"工单获取成功!");
                }
                else
                {
                    MessageBox.Show($"工单获取失败！！！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"工单发生错误,错误信息:{ex.Message}");
            }
        }






        //public string WorkorderSelectedValue { get; set; }
        string workorderSelectedValue;
        public string WorkorderSelectedValue
        {
            get { return workorderSelectedValue; }
            set
            {
                workorderSelectedValue = value;
                RaisePropertyChanged(() => WorkorderSelectedValue);
            }
        }
        string bill_No;
        public string Bill_No
        {
            get { return bill_No; }
            set
            {
                bill_No = value;
                RaisePropertyChanged(() => Bill_No);
            }
        }
        string material_Code;
        public string Material_Code
        {
            get { return material_Code; }
            set
            {
                material_Code = value;
                RaisePropertyChanged(() => Material_Code);
            }
        }
        string material_Spec;
        public string Material_Spec
        {
            get { return material_Spec; }
            set
            {
                material_Spec = value;
                RaisePropertyChanged(() => Material_Spec);
            }
        }
        string pC_No;
        public string PC_No
        {
            get { return pC_No; }
            set
            {
                pC_No = value;
                RaisePropertyChanged(() => PC_No);
            }
        }
        string wrkOrderStatus_Name;
        public string WorkOrderStatus_Name
        {
            get { return wrkOrderStatus_Name; }
            set
            {
                wrkOrderStatus_Name = value;
                RaisePropertyChanged(() => WorkOrderStatus_Name);
            }
        }
        string planedStart;
        public string PlanedStart
        {
            get { return planedStart; }
            set
            {
                planedStart = value;
                RaisePropertyChanged(() => PlanedStart);
            }
        }
        string planedEndDate;
        public string PlanedEndDate
        {
            get { return planedEndDate; }
            set
            {
                planedEndDate = value;
                RaisePropertyChanged(() => PlanedEndDate);
            }
        }
        string productionOrderNO;
        public string ProductionOrderNO
        {
            get { return productionOrderNO; }
            set
            {
                productionOrderNO = value;
                RaisePropertyChanged(() => ProductionOrderNO);
            }
        }








        RelayCommand cmdOrderSelectionChanged;
        public RelayCommand CmdOrderSelectionChanged
        {
            get { return cmdOrderSelectionChanged ?? (cmdOrderSelectionChanged = new RelayCommand(() => DoCmdOrderSelectionChanged())); }
        }
        public void DoCmdOrderSelectionChanged()
        {
            try
            {
                var index = lstBill_No.IndexOf(WorkorderSelectedValue);
                Bill_No = lstBill_No[index];
                Material_Code = lstMaterial_Code[index];
                Material_Spec = lstMaterial_Spec[index];
                PC_No = lstPC_No[index];
                WorkOrderStatus_Name = lstWorkOrderStatus_Name[index];
                PlanedStart = lstPlanedStart[index];
                PlanedEndDate = lstPlanedEndDate[index];
                ProductionOrderNO = lstProductionOrderNO[index];
            }
            catch (Exception)
            {

            }
        }

    }
}
